/*
    BibleNotebook - A bible reader software
    Copyright (C) 2014  Williams Fernando Lopez

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
 */
package com.hexidec.ekit.component;

import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.Action;
import javax.swing.JSpinner;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 *
 * @author Williams Lopez
 */
public class JSpinnerNoFocus extends JSpinner {

    Action action;
    String actionCommand;
    List<ActionListener> actionListener = new ArrayList<ActionListener>();

    public JSpinnerNoFocus() { super(); init(); }
    public JSpinnerNoFocus(Action a) { action = a; init(); }

    private void init() {
        this.setRequestFocusEnabled(false);
        this.setOpaque(false);
        //this.setBorder(BorderFactory.createEmptyBorder());
        this.setBackground(null);

        super.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                fireActionListener((JSpinnerNoFocus)e.getSource());
            }
        });

        this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    }

    public boolean isFocusable() {
        return false;
    }

    public void setActionCommand(String cmd) {
        actionCommand = cmd;
    }

    public void addActionListener(ActionListener l) {
        this.actionListener.add(l);
    }

    private void fireActionListener(JSpinnerNoFocus source) {
        
        ActionEvent e = new ActionEvent(source, (Integer)source.getValue(), source.getValue().toString());
        for (ActionListener l : this.actionListener) {
            l.actionPerformed(e);
        }
        action.actionPerformed(e);
    }
}
